/////////////////////////////////////////////////////////////////////////////
// Name:        ax_menu_library_settings.cpp
// Purpose:     Library settings menu classes
// Author:      Julian Smart
// Modified by:
// Created:     2009-03-20
// RCS-ID:
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "wx/filename.h"

#include "ax/ax_menu_folder.h"
#include "ax/ax_menu_alert.h"

#include "ax_resource/ax_menu_library_settings.h"
#include "ax_resource/ax_resource_library.h"

/*
 * AxResourceLibrarySettingsMenuItem implementation.
 */

IMPLEMENT_DYNAMIC_CLASS( AxResourceLibrarySettingsMenuItem, AxMenuItem )

AxResourceLibrarySettingsMenuItem::AxResourceLibrarySettingsMenuItem(AxResourceLibrary* library, const wxString& label, const wxString& description ,
                                                                     const wxString& id)
{
    wxString itemLabel = label;
    if (itemLabel.IsEmpty())
        itemLabel = _("Resource library settings");
    wxString descr = description;
    if (descr.IsEmpty())
        descr = _("Change resource library settings here.");
    
    SetLabel(itemLabel);
    SetDescription(descr);
    SetId(id);

    SetActivator(new AxResourceLibrarySettingsActivator(library));
}

/*
 * AxResourceLibrarySettingsActivator implementation.
 */

IMPLEMENT_DYNAMIC_CLASS( AxResourceLibrarySettingsActivator, AxActivator )

void AxResourceLibrarySettingsActivator::Copy(const AxResourceLibrarySettingsActivator& activator)
{
    AxActivator::Copy(activator);

    m_library = activator.m_library;
}

AxMenu* AxResourceLibrarySettingsActivator::Activate(AxMenuController* WXUNUSED(controller), AxMenuItem* item)
{
    if (item->GetSubMenu())
        return item->GetSubMenu();

    AxMenu* menu = new AxMenu(_("Library settings"), _("Change resource library settings here."));

    AxFolderMenuItem* menuItem = new AxFolderMenuItem(
        _("Main library path: %FOLDERNAME%"),
        _("Set the location of the main library path, where new files are stored. The current value is: %FOLDERNAME%."),
        wxEmptyString,
        m_library->GetSettings().m_mainLibraryPath);
    menuItem->SetValidator(new AxFolderValidator(& m_library->GetSettings().m_mainLibraryPath)); 
    menu->AddItem(menuItem);

    menu->AddItem(new AxGoUpMenuItem);

    return menu;
}

/*
 * AxResourceLibraryAddByFolderMenuItem implementation.
 */

IMPLEMENT_DYNAMIC_CLASS( AxResourceLibraryAddByFolderMenuItem, AxMenuItem )

AxResourceLibraryAddByFolderMenuItem::AxResourceLibraryAddByFolderMenuItem(AxResourceLibrary* library, const wxString& label, const wxString& description ,
                                                                     const wxString& id)
{
    wxString itemLabel = label;
    if (itemLabel.IsEmpty())
        itemLabel = _("Add resources by folder");
    wxString descr = description;
    if (descr.IsEmpty())
        descr = _("Add one or several resources by scanning a folder.");
    
    SetLabel(itemLabel);
    SetDescription(descr);
    SetId(id);

    SetActivator(new AxResourceLibraryAddActivator(AxResourceLibraryAddActivator::AxResourceLibraryAddByFolderCommand, library));
}

/*
 * AxResourceLibraryAddByFileMenuItem implementation.
 */

IMPLEMENT_DYNAMIC_CLASS( AxResourceLibraryAddByFileMenuItem, AxMenuItem )

AxResourceLibraryAddByFileMenuItem::AxResourceLibraryAddByFileMenuItem(AxResourceLibrary* library, const wxString& label, const wxString& description ,
                                                                     const wxString& id)
{
    wxString itemLabel = label;
    if (itemLabel.IsEmpty())
        itemLabel = _("Add resources by file");
    wxString descr = description;
    if (descr.IsEmpty())
        descr = _("Add a resource from a file.");
    
    SetLabel(itemLabel);
    SetDescription(descr);
    SetId(id);

    SetActivator(new AxResourceLibraryAddActivator(AxResourceLibraryAddActivator::AxResourceLibraryAddByFileCommand, library));
}

/*
 * AxResourceLibraryAddActivator implementation.
 */

IMPLEMENT_DYNAMIC_CLASS( AxResourceLibraryAddActivator, AxActivator )

void AxResourceLibraryAddActivator::Copy(const AxResourceLibraryAddActivator& activator)
{
    AxActivator::Copy(activator);

    m_library = activator.m_library;
    m_command = activator.m_command;
}

AxMenu* AxResourceLibraryAddActivator::Activate(AxMenuController* controller, AxMenuItem* item)
{
    if (m_command == AxResourceLibraryAddByFolderCommand)
    {
        wxString menuLabel = _("Choose the folder to search");
        wxString menuDescription = _("Browse for the folder to scan for new resources.");
        wxString defaultFolder;

        AxModalFolderMenu menu(controller, item, menuLabel, menuDescription, defaultFolder, 0 /* style */);
        
        wxString id = menu.ShowModal();
        
        if (id != AxIdentifierCancel)
        {
            m_library->LoadFromFolder(menu.GetPath(), false, true);
            m_library->RefreshAll();
            m_library->SaveLibrary();
        }
    }
    else if (m_command == AxResourceLibraryAddByFolderCommand)
    {
        wxString menuLabel = _("Choose a resource to add");
        wxString menuDescription = _("Browse for the file to add to the resource library.");
        wxString defaultFolder;

        wxString id, path;
        {
            AxModalFolderMenu menu(controller, item, menuLabel, menuDescription, defaultFolder, axFOLDER_MENU_STYLE_FILE_SELECTOR /* style */);
        
            id = menu.ShowModal();
            path = menu.GetPath();
        }
        
        if (id != AxIdentifierCancel && !path.IsEmpty())
        {
            if (m_library->CreateResourceFromFile(path))
            {
                m_library->RefreshAll();
                m_library->SaveLibrary();
            }
            else
            {
                wxString filename = wxFileNameFromPath(path);

                AxModalAlertMenu alert(controller, item,
                    _("Resource loading problem"),
                    _("There was a problem loading this resource."),
                    wxString::Format(_("Could not load resource %s"), filename.c_str()));
                alert.ShowModal();
            }
        }
    }

    return NULL;
}

